<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/1/25 14:25
 * FileName: Base.php
 */

namespace app\common\model;


use think\Model;

class Base extends Model
{
    protected $connection;
    protected $tip_name;
    protected $validate;
    protected $table;

    protected $res=[
        'code'=>3,
        'msg'=>'验证失败'
    ];

    //添加
    protected function addModel($input){
        if(!$this->validate->scene('create')->check($input)){
            $this->res['msg']=$this->validate->getError();
            return $this->res;
        }

        $this->create_time=request()->time();
        $this->update_time=request()->time();

        //添加数据
        $data=$this->save($input);
        if($data){
            $this->res['code']=0;
            $this->res['id']=$this->getAttr('id');
            $this->res['msg']=$this->tip_name.'添加成功！';
        }else{
            $this->res['msg']=$this->tip_name.'添加失败,请重试！';
        }
        return $this->res;
    }
    //添加
    protected function editModel($input){
		if(isset($input['id']) && $input['id']){
			if(!$this->validate->scene('edit')->check($input)){
				$this->res['msg']=$this->validate->getError();
				return $this->res;
			}
			$input['update_time']=request()->time();
			
			$data=$this->save($input,[$this->pk=>$input['id']]);
		}else{
			foreach($input as $v){
				if(!$this->validate->scene('edit')->check($v)){
					$this->res['msg']=$this->validate->getError();
					return $this->res;
				}
			}
			$data=$this->saveAll($input);
		}
        if($data){
            $this->res['code']=0;
            $this->res['msg']=$this->tip_name.'修改成功！';
        }else{
            $this->res['msg']=$this->tip_name.'修改失败,请重试！';
        }
        return $this->res;
    }
    //删除
    public function delModel($input,$bool=true){
        $ids=array_unique($input);
        if(count($ids)>0) {
            if ($this->destroy($ids,$bool)) {
                $this->res['code'] = 0;
                $this->res['msg'] = '删除'.$this->tip_name.'成功！';
            } else {
                $this->res['msg'] = '删除'.$this->tip_name.'失败,请重试！';
            }
        }else{
            $this->res['msg']='删除'.$this->tip_name.'失败,存在不能删除的数据,请重试！';
        }
        return $this->res;
    }
}